//发表新文章处理函数

const moment = require('moment')
const path = require('path')
const db=require('../db')
exports.addArticle = (req, res) => {
    console.log(req.body);
    console.log(req.file);
   if(!req.file||req.file.fieldname !=='cover_img') return res.cc('请先上传文件')
//    定义sql插入语句
    const sql = `insert into ev_articles set?`
    const art = {
        ...req.body,
        pub_date: moment().format('YYYY-MM-DD HH:mm:ss'),//发布日期
        author_id: req.user.id,
        cover_img: path.join('/uploads', req.file.fieldname),
        
    }
    console.log(art);
    db.query(sql, [ art ], (err, results) => {
      // 执行失败
      if (err) return res.cc(err)
      // 非查询条件需要判断影响的行数
      if (results.affectedRows !== 1) return res.cc('操作失败!')
      // 执行成功 TODO: 
      res.send({
        status: 0,
        message: '发布文章成功'
      })
    })
}


